package com.android.clinic.app.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.clinic.app.R;

import java.util.Map;

/**
 * Created by cheng on 2018/2/5.
 */
public class CustomPickDialog {

    private Context mContext;
    private Dialog mDialog;
    private Display mDisplay;
    private LinearLayout mLayCancel, mLayContent;
    private OnPickItemClickListener mItemClickListener;

    public CustomPickDialog setOnPickItemClickListener(
            OnPickItemClickListener listener) {
        mItemClickListener = listener;
        return this;
    }

    public CustomPickDialog(Context context) {
        mContext = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        mDisplay = windowManager.getDefaultDisplay();
    }

    public CustomPickDialog builder(final Map<String, Object> mapInfo) {
        // 获取Dialog布局
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_pick_layout, null);
        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(mDisplay.getWidth());
        // 定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);
        mDialog.setContentView(view);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        mLayContent = (LinearLayout) view.findViewById(R.id.layout_content);
        mLayCancel = (LinearLayout) view.findViewById(R.id.layout_cancel);
        mLayCancel.setOnClickListener(v -> mDialog.dismiss());
        mLayContent.removeAllViews();
        int labelSize = mapInfo.size();
        for(int i = 0; i< labelSize; i++){
            final String key = (String) mapInfo.keySet().toArray()[i];
            View convertView = LinearLayout.inflate(mContext, R.layout.item_dialog_layout, null);
            final TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
            View line = convertView.findViewById(R.id.line);
            line.setVisibility(i == labelSize - 1 ? View.GONE : View.VISIBLE);
            tvName.setText(mapInfo.get(key).toString());
            tvName.setTag(i);
            tvName.setOnClickListener(v -> {
                if(mItemClickListener != null) {
                    mItemClickListener.onComplete(v, (int) tvName.getTag(), key);
                }
                mDialog.dismiss();
            });
            mLayContent.addView(convertView);
        }
        return this;
    }

    public CustomPickDialog setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    public CustomPickDialog setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public void show() {
        mDialog.show();
    }

    public interface OnPickItemClickListener {
        void onComplete(View v, int position, String id);
    }
}
